package com.hd.appcommon.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hd.appcommon.entity.DdoaBpm;
import com.hd.appcommon.entity.User;
import com.hd.appcommon.entity.vo.SubstituteDetailVo;
import com.hd.appcommon.entity.vo.SubstituteExcelVo;
import com.hd.appcommon.entity.vo.SubstituteVo;
import com.hd.appcommon.entity.vo.SubstituteWebVo;
import com.hd.appcommon.mapper.DdoaBpmMapper;
import com.hd.appcommon.service.DdoaBpmService;
import com.hd.appcommon.service.UserService;
import com.hd.appcommon.utils.DDOAUtil;
import com.hd.hdxgddcommon.page.MyPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
/**
 * (DdoaBpm)表服务实现类
 * 钉钉官方审批流 所有审批的公共数据
 * @author makejava
 * @since 2023-07-06 14:49:15
 */
@Service
public class DdoaBpmServiceImpl extends ServiceImpl<DdoaBpmMapper,DdoaBpm> implements DdoaBpmService {
    @Autowired
    private DdoaBpmMapper ddoaBpmMapper;
    @Autowired
    DDOAUtil ddoaUtil;
    @Autowired
    UserService userService;
    @Autowired
    DdoaBpmService ddoaBpmService;

    //保存审批数据到数据库
    public JSONObject save(String processInstancesId) throws Exception {

        //从接口获取数据
        JSONObject res = ddoaUtil.getProcessInstancesInfo(processInstancesId);

        //修改fastjson默认的日期转换格式，对象转换完成之后再修改回原来的格式
        String DEFFAULT_DATE_FORMAT = JSONObject.DEFFAULT_DATE_FORMAT;
        JSONObject.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
        DdoaBpm ddoaBpm = res.toJavaObject(DdoaBpm.class);
        JSONObject.DEFFAULT_DATE_FORMAT = DEFFAULT_DATE_FORMAT;

        //保存审批实例id和审批模板id
        ddoaBpm.setProcessCode(ddoaUtil.ProcessCode_CourseReplace);
        ddoaBpm.setProcessInstanceId(processInstancesId);

        //添加用户姓名以及工号
        String ddId = ddoaBpm.getOriginatorUserId();
        QueryWrapper wrapper = new QueryWrapper<>()
                .select("name","job_number")
                .eq("ddId",ddId);
        User user = userService.getOne(wrapper);
        ddoaBpm.setName(user.getName());
        ddoaBpm.setJobNumber(user.getJobNumber());

        //插入数据
        ddoaBpmService.save(ddoaBpm);

        return res;
    }

    public JSONObject update(String processInstancesId) throws Exception{
        //从接口获取数据
        JSONObject res = ddoaUtil.getProcessInstancesInfo(processInstancesId);

        //修改fastjson默认的日期转换格式，对象转换完成之后再修改回原来的格式
        String DEFFAULT_DATE_FORMAT = JSONObject.DEFFAULT_DATE_FORMAT;
        JSONObject.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd'T'HH:mm'Z'";
        DdoaBpm ddoaBpm = res.toJavaObject(DdoaBpm.class);
        JSONObject.DEFFAULT_DATE_FORMAT = DEFFAULT_DATE_FORMAT;

        UpdateWrapper updateWrapper = new UpdateWrapper<>()
                .eq("process_instance_id",processInstancesId)
                .set("biz_action",ddoaBpm.getBizAction())
                .set("status",ddoaBpm.getStatus())
                .set("result",ddoaBpm.getResult())
                .set("finish_time",ddoaBpm.getFinishTime());

        ddoaBpmService.update(updateWrapper);

        return res;
    }

    @Override
    public List<SubstituteDetailVo> getDetail(Integer bpmId) {
        List<SubstituteDetailVo> detailVos = new ArrayList<>();
        QueryWrapper<SubstituteDetailVo>queryWrapper1 = new QueryWrapper<>() ,queryWrapper2  = new QueryWrapper<>();
        queryWrapper1.eq("ddoa_bpm.id",bpmId).eq("ddoa_course_replace_course.replace_type",0);
        queryWrapper2.eq("ddoa_bpm.id",bpmId).eq("ddoa_course_replace_course.replace_type",1);
        SubstituteDetailVo v0 = ddoaBpmMapper.getDetail(queryWrapper1);
        SubstituteDetailVo v1 = ddoaBpmMapper.getDetail(queryWrapper2);
        if(v0!=null){
            if(v0.getTerm()!=null&&v0.getTerm().length()>=12){
                v0.setTerm(v0.getTerm().substring(1,12));
            }
            else{
                v0.setTerm("");
            }
        }
        if(v1!=null){
            if(v1!=null&&v1.getTerm()!=null&&v1.getTerm().length()>=12){
                v1.setTerm(v1.getTerm().substring(1,12));
            }
            else{
                v1.setTerm("");
            }
        }

        detailVos.add(v0);
        detailVos.add(v1);
        return detailVos;
    }

    @Override
    public IPage<SubstituteWebVo> getSubstituteVoList(MyPage page, QueryWrapper<SubstituteWebVo> queryWrapper) {
        return ddoaBpmMapper.getSubstituteVoList(page, queryWrapper);
    }

    @Override
    public List<SubstituteExcelVo> getSubstituteVoList2(QueryWrapper<SubstituteWebVo> queryWrapper) {
        return ddoaBpmMapper.getSubstituteVoList2(queryWrapper);
    }
}
